package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"quiz-master/handlers"
)

func Router() *gin.Engine {
	r := gin.Default()
	r.Use(Cors()) //默认跨域
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello World!")
	})
	/////////////权限业务2//////////////////////
	r.POST("/userlogin", handlers.UserLogin)
	r.GET("/userrolelist1", handlers.List1)
	r.GET("/usernodelist1", handlers.UserNodeList1)
	/////////////权限业务1/////////////
	r.POST("/login", handlers.Login)
	r.POST("/userRoleAdd", handlers.RoleUser)
	r.POST("/roleNode", handlers.Add)
	r.GET("/userrolelist", handlers.List)
	r.GET("/usernodelist", handlers.UserNodeList)
	r.GET("/userlist", handlers.UserList)
	r.GET("/userdetail", handlers.UserDetail)

	///////////////仓库
	r.POST("/repadd", handlers.RepAdd)
	r.POST("/reporderadd", handlers.RepOrderAdd)
	r.GET("/repoOrderList", handlers.RepoOrderList)
	r.GET("/repoOrderDetail", handlers.RepoOrderDetail)
	//////////////////货物车辆配送订单///////////////
	r.POST("/caradd", handlers.CarAdd)
	r.POST("/carOrderAdd", handlers.CarOrderAdd)
	r.GET("/carOrderList", handlers.CarOrderList)
	r.GET("/carOrderDetail", handlers.CarOrderDetail)
	//////////////商品订单或者售后订单管理/////////
	r.GET("/goodOrderlist", handlers.GoodOrderList)
	r.GET("/goodOrderDetail", handlers.GoodOrderDetail)

	///////配送订单/////////
	r.GET("/personlist", handlers.PerSonList)
	r.GET("/personOrderDetail", handlers.PerSonOrderDetail)

	///////车辆交易订单//////////
	r.GET("/bigcarlist", handlers.BigCarList)
	r.GET("/bigCarorderList", handlers.BigCarOrderList)
	return r
}
func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
